import time
import webview
import tkinter as tk
import threading


def create_tk_window():
    splash = tk.Tk()
    # splash.withdraw()  # 初始隐藏窗口
    width = 200
    height = 100
    splash.overrideredirect(True)
    splash.geometry("200x100+200+200")
    splash.configure(bg="#f0f0f0")

    # 居中窗口
    screen_width = splash.winfo_screenwidth()
    screen_height = splash.winfo_screenheight()
    x = (screen_width) // 2
    y = (screen_height) // 2

    label = tk.Label(
        splash, text="正在初始化...", font=("Microsoft YaHei", 14), bg="#f0f0f0"
    )
    label.pack(expand=True)

    splash.title("欢迎页")
    splash.geometry(f"+{x}+{y}")
    splash.overrideredirect(True)

    splash.attributes("-topmost", True)  # 设置为置顶
    splash.after(100, lambda: splash.attributes("-topmost", False))  # 短暂置顶后取消
    # 3秒后显示窗口
    # splash.after(1000, splash.deiconify())

    splash.after(3000, lambda: splash.destroy())
    print("-------show Tk")
    tk.mainloop()


def create_webview_window():
    window = webview.create_window(
        "主窗口",
        url="https://www.cnblogs.com",
        width=800,
        height=480,
        frameless=False,
        # easy_drag=True,
        # on_top=True,
        # fullscreen=True,  # 启用全屏模式
        maximized=True,  # 启用最大化模式
        confirm_close=True,  # 关闭时确认
        hidden=True,
    )
    webview.start(show_window, window)


def show_window(window):
    print("-------sleep 3.....")
    time.sleep(3)
    print("-------wake up....")
    window.show()


if __name__ == "__main__":
    # 创建并启动tkinter线程
    tk_thread = threading.Thread(target=create_tk_window)
    tk_thread.daemon = True
    tk_thread.start()

    # 在主线程中启动webview
    create_webview_window()
